#include					"Graphics.hpp"

Graphics::Graphics()
	: _window(new sf::RenderWindow(sf::VideoMode(WIN_SIZE, WIN_SIZE), WIN_NAME)), _screenPos(0)
{
	this->init();
}

Graphics::~Graphics()
{
}

void                        Graphics::run()
{
	while (_window->isOpen())
    {
        eventManager();
        display();   
    }
}

void                        Graphics::eventManager()
{
	sf::Event               event;
	int						ret;

	while (_window->pollEvent(event))
	{
		if (event.type == sf::Event::Closed)
			_window->close();
		else if ((event.type == sf::Event::MouseButtonPressed))
		{
			if (_screens[_screenPos]->mouseButtonEvent(event.mouseButton) && _screenPos == 0)
				_screens[++_screenPos]->init(_screens[0]->getGameType());
			if (_screenPos == 1 && (ret = _screens[_screenPos]->isThereAWinner() >= 0))
				_screens[++_screenPos]->init(_screens[1]->getWinner(ret));
			//else if (_screenPos == 2)
			//{
			//	this->clear();
			//	this->init();
			//	_screens[_screenPos]->init(_screens[0]->getGameType());
			//}
		}
		else if ((event.type == sf::Event::MouseMoved) && _screenPos == 0)
			_screens[_screenPos]->mouseMoveEvent(event.mouseMove);


	}
}

void                        Graphics::display()
{
	_window->clear();
	_screens[_screenPos]->draw();
	_window->display();
}

void						Graphics::init()
{
	_screens.push_back(new Menu(*_window));
	_screens.push_back(new GameScreen(*_window));
	_screens.push_back(new Winning(*_window));
}

void						Graphics::clear()
{
	_screenPos = 0;
	_screens.clear();
}